package com.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.config.CSCWebSocket;
import com.project.dto.UpdateDiscountDto;
import com.project.entity.ChargeRecordEntity;
import com.project.entity.PercentEntity;
import com.project.entity.UserEntity;
import com.project.entity.UserOrderEntity;
import com.project.mapper.IChargeRecordMapper;
import com.project.mapper.IPercentMapper;
import com.project.mapper.IUserMapper;
import com.project.service.IUserRechargeService;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@Transactional
@RequestMapping("UserRecharge")
public class UserRechargeController {

    @Autowired
    private IUserRechargeService service;
    @Autowired
    private IUserService userService;
    @Autowired
    IUserMapper userMapper;
    @Autowired
    IChargeRecordMapper chargeRecordMapper; //用户充值记录
    @Autowired
    IPercentMapper percentMapper;//折扣率
    @Autowired
    CSCWebSocket cscWebSocket;
    @RequestMapping("buy")
    public String buy(UserOrderEntity userOrderEntity) {
       service.buy(userOrderEntity);
       return "ok";
    }

    @RequestMapping("findByItem")
    public IPage<ChargeRecordEntity> findByItem(int pageNO, int pageSize) {
        return service.findByItem(pageNO,pageSize);
    }


    //TODO 用户充值
    @RequestMapping("findById")
    public ChargeRecordEntity findById(Integer id) {
        return service.findById(id);
    }

    @RequestMapping("addMoney/{userNum}/{addMoney}")
    public UpdateDiscountDto updateUserDiscount(@PathVariable("userNum") Long userNum, @PathVariable("addMoney")Double addMoney) throws IOException {

                //获得充值用户对象
                QueryWrapper<UserEntity> qw = new QueryWrapper<>();
                qw.eq("u_num",userNum);
                UserEntity userEntity = userMapper.selectOne(qw);
                double newBalance = userEntity.getBalance() + addMoney;//新的余额
                ChargeRecordEntity chargeRecordEntity=new ChargeRecordEntity();
                chargeRecordEntity.setName(userEntity.getName());
                chargeRecordEntity.setUserNum(userNum);
                chargeRecordEntity.setMoney(addMoney);
                ChargeRecordEntity newChargeRecord = chargeRecordMapper.findNewChargeRecord(userNum);
                if (newChargeRecord == null) {//用户为第一次充值
                    chargeRecordEntity.setTotalMoney(addMoney);
                    chargeRecordEntity.setBalance(addMoney);
                }else {
                    double totalChargeMoney =   (newChargeRecord.getTotalMoney() + addMoney);//总充值金额
                    chargeRecordEntity.setTotalMoney(totalChargeMoney);
                }

                chargeRecordEntity.setBalance(newBalance);
                userEntity.setBalance(newBalance);


                chargeRecordMapper.insert(chargeRecordEntity);
                //查询折扣率表
                QueryWrapper<PercentEntity> recordEntityQueryWrapper = new QueryWrapper<>();
                recordEntityQueryWrapper.ge("p_endMoney", chargeRecordEntity.getTotalMoney());
                recordEntityQueryWrapper.le("p_startMoney", chargeRecordEntity.getTotalMoney());
                PercentEntity percentEntity = percentMapper.selectOne(recordEntityQueryWrapper);
                //更新折扣率和余额
                userEntity.setDiscount(percentEntity.getDiscount());
                userMapper.updateById(userEntity);
                cscWebSocket.SendMessage(CSCWebSocket.SUPPLIER_PATH);//通知页面来消息了

                UpdateDiscountDto updateDiscountDto=new UpdateDiscountDto();
                updateDiscountDto.setDiscount(userEntity.getDiscount());
                updateDiscountDto.setUserNum(userEntity.getUserRef());
                updateDiscountDto.setTotalPrice(userEntity.getBalance());
            return updateDiscountDto;


    }
}
